﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<style>

    .layui-table-tool > .layui-table-tool-temp {
        padding-right: 0px;
    }
</style>
<script>
    layui.use(['jquery', 'form', 'table', 'common', 'commonTable', 'optimizeSelectOption'], function () {
        var $ = layui.jquery,
            form = layui.form,
            commonTable = layui.commonTable,
            table = layui.table,
            common = layui.common;
        var itemId = $.request("itemId");
        commonTable.rendertable({
            elem: '#currentTable',
            id: 'currentTable',
            defaultToolbar: [],
            data: [],
            search: false,
            limit: 9999,//每页数据 默认
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count'] //自定义分页布局
                , first: false //不显示首页
                , last: false //不显示尾页
            },
            height: 'full-130',
            cols: [[
                { type: "checkbox", width: 50 },
                {
                    field: 'F_MaterialType', title: '料号分类', width: 120,
                    templet: function (d) {
                        return top.clients.dataItems["Mes_MaterialType"][d.F_MaterialType] == null ? "" : top.clients.dataItems["Mes_MaterialType"][d.F_MaterialType];
                    }
                },
                { field: 'F_MaterialCode', title: '料号编号', width: 170 },
                { field: 'F_MaterialName', title: '料号名称', minWidth: 200 },
                { field: 'F_Num', title: '数量', width: 120, edit: 'text' },
                { field: 'F_Description', title: '备注', width: 120, edit: 'text' },
            ]]
        });
        $("#F_ProcessId").bindSelect({
            url: "/ProcessManage/ProcessFlow/GetListJson",
            id: "F_Id",
            text: "F_ProcessFlowName"
        }); 
        $(function () {
            common.ajax({
                url: '/MaterialManage/Material/GetFormJson',
                dataType: 'json',
                data: { keyValue: itemId },
                async: false,
                success: function (data) {
                    parent.$(".layui-layer-title")[0].innerText = '添加工序及构成，生成物料（' +data.F_MaterialCode+':'+ data.F_MaterialName + '）';
                }
            });
            form.render();
        });
        form.render();
        //加载数据
        wcLoading.close();
        //行点击事件监听，控制按钮显示
        var moreList = ["NF-delete"];//选择多条显示
        commonTable.tableRowClick("checkbox", "currentTable", "currentTable", null, moreList);

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTable)', function (obj) {
            var data = table.checkStatus('currentTable').data;
            if (obj.event === 'add') {  // 监听操作
                if (!itemId) {
                    return false;
                }
                var parentform = window.name;
                common.modalOpen({
                    title: "添加绑定",
                    url: "/MaterialManage/BomInfo/AddForm?parentform=" + parentform+"&itemId=" + itemId,
                    width: "400px",
                    height: "400px",
                });
            }
            else if (obj.event === 'delete') {
                if (data.length==0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                common.modalConfirm("注：您确定要删除该项数据吗？", function (r) {
                    if (r) {
                        var index = parent.layer.load(0, {
                            shade: [0.5, '#000'], //0.1透明度的背景
                        });
                        try {
                            var cachedata = table.cache.currentTable;
                            for (var j = 0; j < data.length; j++) {
                                for (var i = 0; i < cachedata.length; i++) {
                                    if (data[j].F_Id == cachedata[i].F_Id) {
                                        cachedata.splice(i, 1);
                                        break;
                                    }
                                }
                            }

                            table.reload('currentTable', {
                                data: cachedata
                            });
                            entity = null;
                            common.modalMsg("操作成功", "success");
                        } catch (e) {
                            alert(e);
                        }
                        parent.layer.close(index);
                    }
                    else {
                        return false;
                    }
                });
            }
            return false;
        });
        // 监听搜索操作
        form.on('submit(saveBtn)', function (data) {
            if (!itemId) {
                common.modalMsg("未选择主物料", "worning");
                return false;
            }
            if (!$('#F_ProcessId').val()) {
                common.modalMsg("未选择工序", "warning");
                return false;
            }
            var cachedata = table.cache.currentTable;
            var postData = data.field;
            postData.F_ProcessId=$('#F_ProcessId').val();
            postData["F_MaterialId"] = itemId;
            if (cachedata.length != 0) {
                postData.listData = JSON.stringify(cachedata);
            }
            common.submitForm({
                url: '/MaterialManage/BomInfo/SubmitForm',
                param: postData,
                success: function () {
                    common.parentreload('data-search-btn');
                }
            })
            return false;
        });
    });

</script>
<div class="layui-fluid" style="padding:0 0px">
    <div class="layui-row layui-col-space5">
        <div class="layui-card" style="padding: 5px;">
            <fieldset class="table-search-fieldset" style="border:1px solid #e6e6e6">
                <legend>工序及构成</legend>
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">选择工序</label>
                        <div class="layui-inline">
                            <select id="F_ProcessId" name="F_ProcessId" lay-search>
                                <option value="" selected>请选择</option>
                            </select>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="temp-search-btn"><i class="layui-icon">&#xe615;</i> 刷 新</button>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <button class="layui-btn" lay-submit id="submit" lay-filter="saveBtn">确认保存</button>
                    </div>
                </form>
            </fieldset>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container" id="toolbar">
                    <button id="NF-add" class="layui-btn layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon">&#xe654;</i>新建</button>
                    <button id="NF-delete" name="NF-delete" class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn layui-hide" lay-event="delete"> <i class="layui-icon">&#xe640;</i>删除</button>
                </div>
            </script>
            <table class="layui-hide" id="currentTable" lay-filter="currentTable"></table>
        </div>
    </div>
</div>
